/* The code examples copyrighted by Nokia Corporation that are included to
 * this material are licensed to you under following MIT-style License:
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
import QtQuick 1.1
import com.nokia.meego 1.0
import com.nokia.extras 1.0

Page {
    id: container
    anchors.margins: 10
    tools: commonTools

    QueryDialog {
        id: errorDialog
        titleText: "Error"
        acceptButtonText: "Ok"
    }

    Flickable {
        flickableDirection: Flickable.VerticalFlick
        pressDelay: 100

        Column {
            spacing: 2
            Grid {
                columns: 2
                spacing: 2
                Label {
                    text: "Event date:"
                }
                Row {
                    Button {
                        id: eventDateButton
                        text: "Click to set"
                        width: container.width/3 - 20
                        onClicked: eventDateDialog.open()
                    }
                    Button {
                        id: eventEndDateButton
                        text: "Click to set"
                        width: container.width/3  - 20
                        enabled: recurrenceSelectionDialog.selectedIndex > 0 ? true : false
                        onClicked: eventEndDateDialog.open()
                    }
                }
                Label {
                    text: "Event time:"
                }
                Row {
                    Button {
                        id: startTimeButton
                        text: "Click to set"
                        width: container.width/3 - 20
                        onClicked: launchStartTimeDialog()
                    }
                    Button {
                        id: endTimeButton
                        text: "Click to set"
                        width: container.width/3 - 20
                        onClicked: launchEndTimeDialog()
                    }
                }

                Label {
                    text: "Event summary:"
                }
                TextField {
                    id: eventSummary
                }

                Label {
                    text: "Event description:"
                    verticalAlignment: Text.AlignVCenter
                }
                TextField {
                    id: eventDescription
                }

                Label {
                    text: "Event location:"
                    verticalAlignment: Text.AlignVCenter
                }
                TextField {
                    id: eventLocation
                }

                Label {
                    text: "Recurring:"
                }
                Button {
                    id: recurrenceButton
                    text: recurrenceSelectionDialog.model.get(recurrenceSelectionDialog.selectedIndex).name
                    width: eventSummary.width
                    onClicked: {
                        recurrenceSelectionDialog.open();
                    }
                }

            }


            Button {
                id: addEventButton
                text: "Add Event"
                onClicked: {
                    if (eventDateButton.text == "Click to set") {
                        errorDialog.message = "Event date not set";
                        errorDialog.open();
                    } else if (recurrenceSelectionDialog.selectedIndex > 0 && eventEndDateButton.text == "Click to set") {
                        errorDialog.message = "Ending date for the recurrent even not set";
                        errorDialog.open();
                    } else if (startTimeButton.text == "Click to set" || endTimeButton.text == "Click to set") {
                        errorDialog.message = "Event time not set";
                        errorDialog.open();
                    } else {

                        if (recurrenceSelectionDialog.selectedIndex == 0) eventEndDateButton.text = eventDateButton.text

                        if (CalendarExample.AddEvent(eventSummary.text, eventDescription.text, eventLocation.text, eventDateButton.text, eventEndDateButton.text, startTimeButton.text, endTimeButton.text, recurrenceSelectionDialog.selectedIndex)) {
                            pageStack.pop();
                        }
                        else {
                            errorDialog.message = "Failed to add event";
                            errorDialog.open();
                        }
                    }
                }
            }

        }
    }

    DatePickerDialog {
        id: eventDateDialog
        titleText: "Event date"
        onAccepted: setEventDate()
    }

    DatePickerDialog {
        id: eventEndDateDialog
        titleText: "Reccurent event end date"
        onAccepted: setEventEndDate()
    }

    function setEventDate() {
        eventDateButton.text = eventDateDialog.year + "-" + (eventDateDialog.month < 10 ? "0" : "" ) + eventDateDialog.month + "-" + (eventDateDialog.day < 10 ? "0" : "" ) + eventDateDialog.day
    }

    function setEventEndDate() {
        eventEndDateButton.text = eventEndDateDialog.year + "-" + (eventEndDateDialog.month < 10 ? "0" : "" ) + eventEndDateDialog.month + "-" + (eventEndDateDialog.day < 10 ? "0" : "" ) + eventEndDateDialog.day
    }

    function launchStartTimeDialog() {
        startTimeDialog.open();
    }

    function setStartTime() {
        startTimeButton.text = (startTimeDialog.hour < 10 ? "0" : "" ) + startTimeDialog.hour + ":" + (startTimeDialog.minute < 10 ? "0" : "" ) + startTimeDialog.minute  + ":" + (startTimeDialog.second < 10 ? "0" : "") + startTimeDialog.second;
    }

    TimePickerDialog {
        id: startTimeDialog
        titleText: "Event start time"
        acceptButtonText: "CONFIRM"
        rejectButtonText: "CANCEL"
        onAccepted: setStartTime()
    }

    function launchEndTimeDialog() {
        endTimeDialog.open();
    }

    function setEndTime() {
        endTimeButton.text = (endTimeDialog.hour < 10 ? "0" : "" ) + endTimeDialog.hour + ":" + (endTimeDialog.minute < 10 ? "0" : "" ) + endTimeDialog.minute  + ":" + (endTimeDialog.second < 10 ? "0" : "") + endTimeDialog.second;
    }

    TimePickerDialog {
        id: endTimeDialog
        titleText: "Event end time"
        acceptButtonText: "CONFIRM"
        rejectButtonText: "CANCEL"
        onAccepted: setEndTime()
    }

    DatePickerDialog {
        id: dateDialog
        titleText: "Select date"
        onAccepted: setViewDate()
    }

    SelectionDialog {
        id: recurrenceSelectionDialog
        titleText: "Recurrence"
        selectedIndex: 0
        model: ListModel {
            ListElement { name: "One-time event" }
            ListElement { name: "Every day" }
            ListElement { name: "Every week" }
            ListElement { name: "Every year" }
        }
    }

}
